package com.tozed.android.launcher.simpleui;

import java.util.ArrayList;

import com.tozed.android.launcher.LauncherApplication;
import com.tozed.android.launcher.R;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DownLoadAppView extends LinearLayout
{
	private static final String TAG = "DownLoadAppView";
	private LauncherApplication mApplication;
	private Context mContext;
	private DownLoadAdapter mDownLoadAdapter;
	private ArrayList<SimpleApplicationInfo> mDownLoadApps = new ArrayList<SimpleApplicationInfo>();
	private GridView mDownLoadGridView;
	private TextView mEmptyTextView;

	public DownLoadAppView(Context context){
		this(context, null, 0);
	}

	public DownLoadAppView(Context context, AttributeSet attrs){
		this(context, null, 0);
	}

	public DownLoadAppView(Context context, AttributeSet attrs, int defStyle){
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context){
		mContext = context;
	}
	
	public void checkIsEmpty(TextView textView){
		mDownLoadGridView.setAdapter(mDownLoadAdapter);
		setEmptyText(textView);
	}
	
	protected void onFinishInflate()
	{
		super.onFinishInflate();
		
		mDownLoadApps = SimpleLauncherModel.sDownloadApps;
		mApplication = LauncherApplication.getActiveInstance();
		mDownLoadGridView = ((GridView) findViewById(R.id.download_grid));
		mDownLoadAdapter = new DownLoadAdapter(mDownLoadApps);
		mDownLoadGridView.setAdapter(mDownLoadAdapter);
		mDownLoadGridView.addOnLayoutChangeListener(new View.OnLayoutChangeListener()
		{
			public void onLayoutChange(View view, int arg1, int arg2, int arg3, 
					int arg4, int arg5, int arg6, int arg7, int arg8)
			{
				if (mEmptyTextView != null){
					if (mDownLoadAdapter.getCount() <= 0)
						return;
					
					if (mDownLoadAdapter.getCount() == 0 )
						mEmptyTextView.setVisibility(View.VISIBLE);
					else
						mEmptyTextView.setVisibility(View.GONE);
				}
			}
		});
	}
	
	public void setContext(Context context){
	    mContext = context;
	}
	
	public void setEmptyText(TextView textView)
	{
		if (textView == null){
			Log.w(TAG, "not found textView ID is no_download_apps");
			return;
		}
		
		mEmptyTextView = textView;
		if (mDownLoadAdapter.getCount() == 0){
			textView.setVisibility(View.VISIBLE);
		}else{
			textView.setVisibility(View.GONE);
		}
	}
	
	public void updateDownLoadApp(SimpleApplicationInfo app, boolean paramBoolean){
		mDownLoadAdapter.notifyDataSetChanged();
	}

	
	public class DownLoadAdapter extends ArrayAdapter<SimpleApplicationInfo>
	{
		private final LayoutInflater mInflater;
		
		public DownLoadAdapter(ArrayList<SimpleApplicationInfo> appList){
			super(mContext, 0, appList);
			mInflater = LayoutInflater.from(mContext);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			if (convertView == null){
				convertView = (SimpleBubbleTextView)mInflater.inflate(R.layout.simple_activity_application_item, parent, false);
			}
			
			SimpleApplicationInfo appInfo = (SimpleApplicationInfo)mDownLoadApps.get(position);
			
			final SimpleBubbleTextView bubbleView = ((SimpleBubbleTextView) convertView);
			bubbleView.applyFolderIconFromBubbleTextView(appInfo, mApplication.getSimpleIconCache());
			
			convertView.setOnClickListener((AddApplication)mContext);
			return convertView;
		}
	}
}
